<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>浏览器</title>

    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/yzl.css"/>

    <style>
        .browser-close {
            display: inline-block;
            position: absolute;
            width: 30px;
            height: 45px;
            top: 15px;
            left: 40px;
            background-size: 17px 17px;
            background-position: left;
            background-repeat:no-repeat;
        }

        .browser-close.big {
            background-image: url(../../image/big/btn_close_white.png);
        }

        .browser-close.small {
            background-image: url(../../image/small/btn_close_white.png);
        }

        .browser-title {
            margin: 0 60px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
    </style>

</head>
<body>
<div class="header" id="app">
    <div id="back" class="back" style="width:30px;" :class="BSImg" onclick="back()"></div>
    <div v-if="goods_info !==''" v-myclick="[goods_info.share, share]" style="width:20px;float:right;margin-right:10px;margin-top:5px" :class="BSImg"><img width="100%" v-src="'../../image/share_button.png'" tapmode></div>
    <div id="browser-close" class="browser-close" :class="BSImg" v-show="changeTime >= 2" onclick="closeWin()"></div>
    <div v-if="goods_info !==''" class="title browser-title">友人福利购</div>
    <div v-if="goods_info ==''" class="title browser-title">{{title}}</div>
</div>
</body>
<script src="../../script/api.js"></script>
<script src="../../script/vue.js"></script>
<script src="../../script/app.js"></script>
<script src="../../script/common.js"></script>
<script src="../../script/url.js"></script>
<script>

    var webBrowser,
        url,
        goods_id,
        goods_info = '',
        vm;

    apiready = function () {
        setBSImg();
        var back = document.getElementById('back');
        var browserClose = document.getElementById('browser-close');
        setTapmode([back, browserClose]);
        webBrowser = api.require('webBrowser');
        MNActionButton = api.require('PIActionButton');
        wx = api.require('wx');
        goods_id = api.pageParam.goods_id;
        eventId = api.pageParam.buyshareeventId;
        category_id = api.pageParam.category_id;
        eventLable = api.pageParam.eventLable;
        if (goods_id) {
            url = getPrefs("wap_url") + "goods/goodsinfo?goods_id=" + goods_id;
            if (!getPrefs('tipsOfOpenGoodsDetail')) {
                api.confirm({
                    title: '提示',
                    msg: '您需要点击立即领券后直接购买,否则无效!',
                    buttons: ['不再提示', '确定']
                }, function (ret) {
                    if (ret.buttonIndex == 1) {
                        setPrefs('tipsOfOpenGoodsDetail', true);
                    }
                });
            }
            var data = {
            member_id: getPrefs('member_id'),
            goods_id: goods_id
        };
        var callback = function (ret) {
              goods_info = ret.content;
              // console.log(JSON.stringify(goods_info.share));
            initApp();
            api.parseTapmode();
        };
        pinet.post(URL.getGoodsInfo, data, callback);
        } else {
            url = api.pageParam.url;
            initApp();
        }
        // initApp();
        openWeb();
        api.parseTapmode();
    };

    function initApp() {
        vm = new Vue({
            el: '#app',
            data: {
                changeTime: 0,
                BSImg: {
                    big: Vue.BSImg == 'big',
                    small: Vue.BSImg == 'small'
                },
                title: '正在加载中...',
                goods_info:goods_info,
                eventId:eventId,
                category_id:category_id,
                eventLable:eventLable,
                goods_id:goods_id,
            },
            methods: {
        share: function (share) {
            var attributes = {
                category_id: category_id,
                goods_id:goods_id
            };
            var sharesuffix = {
              eventId:eventId,
              eventLable:eventLable+'-购买-分享',
              attributes:attributes
            }
            // onEvent(buyshareeventId:buyshareeventId,eventLable+'-购买-分享',attributes);
            openMNActionButton(share,sharesuffix);
        },
    },
        });
    }

    function back() {
        webBrowser.historyBack(function (ret) {
            if (!ret.status) {
                closeWin();
            }
        });
    }

    function openWeb() {
        var forbid_urlschemes = null;
        if (getPrefs('forbid_urlschemes')) {
            forbid_urlschemes = JSON.parse(getPrefs('forbid_urlschemes'));
        }
        webBrowser.openView({
            url: url,
            rect: {
                x: 0,
                y: 60,
                w: 'auto',
                h: 'auto'
            },
            forbid_urlschemes: forbid_urlschemes || ["tbopen", "tmall", "taobao", "openapp.jdmoble"],
        }, function (ret, err) {

            switch (ret.state) {            //加载状态，数字类型，取值范围：0-开始加载；1-加载进度发生变化；2-结束加载；3-title发生变化；4-url发生变化
                case 0:
                    if (goods_id) {
                        var data = {url: escape(ret.url), member_id: getPrefs('member_id'), goods_id: goods_id};
                        pinet.post(URL.dealOrders, data, function (ret) {
                            // console.log('ret:' + JSON.stringify(ret));
                        });
                    }
                    break;
                case 1:
                    break;
                case 2:
                    vm.changeTime++;
                    break;
                case 3:
                    vm.title = ret.title;
                    break;
                case 4:
                    break;
                case 5:
                    var success = ret.success;
                    if(success && goods_id){
                        var data = {
                            orders:success,
                            member_id:getPrefs('member_id'),
                            goods_id:goods_id
                        };
                        pinet.post(URL.dealTaobaoOrder, data);
                    }
                    break;
                case 6:
                    break;
                default:
                    break;
            }
        })
    }

</script>
</html>
